package com.ask.chatApis;


import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;



public class DeepSeekUtil {
    private static final String BASE_URL = "https://api.deepseek.com/chat/completions";
    private static final String API_KEY = "Bearer sk-21a9efacd4cc4f8886a20dccae0e97ae";


    public static void sendMessage() throws IOException {

        // 创建post请求
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost post = new HttpPost(BASE_URL);

        // 设置请求头
        post.setHeader("Content-Type", "application/json");
        post.setHeader("Authorization", API_KEY);

        // 设置请求体
        String  question= "今天是星期几。 ";
        String requestBody = String.format(
                "{\"model\": \"deepseek-chat\", \"messages\": [{\"role\": \"user\", \"content\": \"%s\"}], \"stream\": false}", question
        );


        StringEntity entity = new StringEntity(requestBody, "UTF-8");
        post.setEntity(entity);

        // 执行请求
        CloseableHttpResponse response = httpClient.execute(post);
        int statusCode = response.getStatusLine().getStatusCode();
        System.out.printf("状态响应码为："  + statusCode);

        if (statusCode != 200) {
            throw new RuntimeException("Failed : HTTP error code : " + statusCode);
        }

        // 获取解析并响应
        HttpEntity entity1 = response.getEntity();
        String body = EntityUtils.toString(entity1);
        System.out.printf("响应数据为：" + body);
        response.close();
        httpClient.close();
    }

    /**
     * java.net.http.HttpClient 版本
     * @throws IOException
     */
    public static void sendMessage2() throws IOException, InterruptedException {
        JSONObject jsonObject = getJsonObject();
        String requestBody = jsonObject.toString();
        HttpClient httpClient = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(BASE_URL))
                .POST(HttpRequest.BodyPublishers.ofString(requestBody))
                .header("Content-Type", "application/json")
                .header("Authorization", API_KEY)
                .build();

        HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
        System.out.println("Status Code: " + response.statusCode());
        System.out.println("Response Body: " + response.body());
    }

    /**
     * 构建请求体
     * @return
     */
    private static JSONObject getJsonObject() {
        JSONObject message = new JSONObject();
        message.put("role", "user");
        message.put("content", "hello");

        JSONArray messages = new JSONArray();
        messages.put(message);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("model", "deepseek-chat");
        jsonObject.put("messages", messages);
//        jsonObject.put("max_tokens", 1688);
//        jsonObject.put("temperature", 0.5);
        jsonObject.put("stream", false);
        return jsonObject;
    }



    public static void main(String[] args) throws IOException, InterruptedException {
        sendMessage2();
    }

}
